﻿using System;
using System.Collections.Generic;
using System.Text;
using HtmlAgilityPack;
using OccupierArchiver.Helpers;

namespace OccupierArchiver.DataClasses
{
    internal static class MessageDao
    {
        private static void CleanEmailScripts(HtmlNode htmlNode)
        {
            var nodes = htmlNode.SelectNodes("a[@class='__cf_email__']");
            if (nodes == null) { return; }
            foreach (var a in nodes)
            {
                if (!String.IsNullOrWhiteSpace(a.Attributes["data-cfemail"].Value))
                {
                    var encoded = a.Attributes["data-cfemail"].Value.Trim();
                    var decoded = CloudFlareBuster.DecodeEmail(encoded);
                    var newnode = HtmlNode.CreateNode(decoded);
                    var nextSibling = a.NextSibling;
                    if (nextSibling.Name == "script")
                    {
                        nextSibling.Remove();
                    }
                    a.ParentNode.ReplaceChild(newnode, a);
                }
            }
        }

        public static int GetPageCount(string path)
        {
            var result = 0;

            var doc = new HtmlDocument();
            doc.Load(path, Encoding.GetEncoding("utf-8"));

            foreach (var div in doc.DocumentNode.SelectNodes("descendant::div[@class='results']"))
            {
                foreach (var p in div.ChildNodes)
                {
                    foreach (var a in p.ChildNodes)
                    {
                        if (a.Name != "a") continue;
                        int page;
                        Int32.TryParse(a.InnerText, out page);
                        if (result < page) result = page;
                    }
                }
            }
            return result;
        }

        public static IEnumerable<MessageDto> GetListMessage(string path)
        {
            var result = new List<MessageDto>();

            var doc = new HtmlDocument();
            doc.Load(path, Encoding.GetEncoding("utf-8"));

            foreach (var table in doc.DocumentNode.SelectNodes("descendant::table[@class='messageTable']"))
            {
                var count = -1;
                foreach (var tr in table.ChildNodes)
                {
                    var msg = new MessageDto();
                    foreach (var td in tr.ChildNodes)
                    {
                        CleanEmailScripts(td);
                        var tdclass = td.GetAttributeValue("class", "");
                        switch (tdclass)
                        {
                            case "messageName":
                                msg.Name = td.InnerText;
                                break;
                            case "messageLocation":
                                msg.Location = td.InnerText;
                                break;
                            case "messageMsg":
                                msg.Message = td.InnerText;
                                break;
                        }
                    }
                    if (msg.IsValid())
                    {
                        count++;
                        if (count == 0) continue;
                        result.Add(msg);
                    }
                }
            }

            return result;
        }
    }
}
